﻿/*
** Haaf's Game Engine 1.5
** Copyright (C) 2003-2005, Relish Games
** hge.relishgames.com
**
** Tutorial 06 - Creating menus
*/

// In menuitem.cpp/h we define the
// behaviour of our custom GUI control
module core.menuitem;

private	import		core.hgeall;
private	import		core.hgegui_bak;
//private	import		core.hgeguictrls;
/*
#include "..\..\include\hge.h"
#include "..\..\include\hgegui.h"
#include "..\..\include\hgeFontCN.h"
#include "..\..\include\hgecolor.h"
*/

class hgeGUIMenuItem : hgeGUIObject
{
public:
	this(int _id, hgeFontCN _fnt, HEFFECT _snd, float _x, float _y, float _delay, char[] _title)
		{
			float w;
			
			id=_id;
			fnt=_fnt;
			snd=_snd;
			delay=_delay;
			title=std.utf.toUTF16(_title);		

			gtxt=_title;

			color.SetHWColor(0xFFFFE060);
			shadow.SetHWColor(0x300000FF);
			offset=0.0f;
			timer=-1.0f;
			timer2=-1.0f;

			bStatic=false;
			bVisible=true;
			bEnabled=true;

			w=fnt.GetStringWidth(title);
			rect.Set(_x-w/2, _y, _x+w/2, _y+fnt.GetHeight());

			
		}
		
	void	Render()
		{
			float fw= fnt.GetStringWidth(std.utf.toUTF16(this.gtxt));
			float fh= fnt.GetHeight();
			
			hgeFontCN.vmes( "字体长宽是==", fnt.GetStringWidth("中"),",",fnt.GetHeight," 是否可见=",this.bVisible,"  是否激活=",this.bEnabled);
			fnt.SetColor(shadow.GetHWColor());


			hge.Gfx_RenderQuad(&hgeQuad(rect.x1+offset+3, rect.y1+3,fw,fh, 0xff0000ff));
			

			fnt.Render(rect.x1+offset+3, rect.y1+3, HGETEXT_LEFT, title);
			fnt.SetColor(color.GetHWColor());
			fnt.Render(rect.x1-offset, rect.y1-offset, HGETEXT_LEFT, title);
			
		}
	void	Update(float dt)
		{
			if(timer2 != -1.0f)
			{
				timer2+=dt;
				if(timer2 >= delay+0.1f)
				{
					color=scolor2+dcolor2;
					shadow=sshadow+dshadow;
					offset=0.0f;
					timer2=-1.0f;
				}
				else
				{
					if(timer2 < delay) { color=scolor2; shadow=sshadow; }
					else { color=scolor2+dcolor2*(timer2-delay)*10; shadow=sshadow+dshadow*(timer2-delay)*10; }
				}
			}
			else if(timer != -1.0f)
			{
				timer+=dt;
				if(timer >= 0.2f)
				{
					color=scolor+dcolor;
					offset=soffset+doffset;
					timer=-1.0f;
				}
				else
				{
					color=scolor+dcolor*timer*5;
					offset=soffset+doffset*timer*5;
				}
			}
		}
		

	void	Enter()
		{
			hgeColor tcolor2;

			scolor2.SetHWColor(0x00FFE060);
			tcolor2.SetHWColor(0xFFFFE060);
			dcolor2=tcolor2-scolor2;

			sshadow.SetHWColor(0x00000000);
			tcolor2.SetHWColor(0x30000000);
			dshadow=tcolor2-sshadow;

			timer2=0.0f;
		}
	void	Leave()
		{
			hgeColor tcolor2;

			scolor2.SetHWColor(0xFFFFE060);
			tcolor2.SetHWColor(0x00FFE060);
			dcolor2=tcolor2-scolor2;

			sshadow.SetHWColor(0x30000000);
			tcolor2.SetHWColor(0x00000000);
			dshadow=tcolor2-sshadow;

			timer2=0.0f;
		}
	bool	IsDone()
		{
			if(timer2==-1.0f) return true;
			else return false;
		}
	void	Focus(bool bFocused)
		{
			hgeColor tcolor;
			
			if(bFocused)
			{
				hge.Effect_Play(snd);
				scolor.SetHWColor(0xFFFFE060);
				tcolor.SetHWColor(0xFFFFFFFF);
				soffset=0;
				doffset=4;
			}
			else
			{
				scolor.SetHWColor(0xFFFFFFFF);
				tcolor.SetHWColor(0xFFFFE060);
				soffset=4;
				doffset=-4;
			}

			dcolor=tcolor-scolor;
			timer=0.0f;
		}
	void	MouseOver(bool bOver)
		{
			if(bOver) gui.SetFocus(id);
		}

	bool	MouseLButton(bool bDown)
		{
			if(!bDown)
			{
				offset=4;
				return true;
			}
			else 
			{
				hge.Effect_Play(snd);
				offset=0;
				return false;
			}
		}
	bool	KeyClick(int key, int chr)
		{
			if(key==HGEK_ENTER || key==HGEK_SPACE)
			{
				MouseLButton(true);
				return MouseLButton(false);
			}
			return false;
		}

private:
	hgeFontCN	fnt;
	HEFFECT		snd;
	float		delay;
	wchar[]	title;


	hgeColor	scolor, dcolor, scolor2, dcolor2, sshadow, dshadow;
	hgeColor	color, shadow;
	float		soffset, doffset, offset;
	float		timer;
	float 		timer2;
}